﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace Love.Awesome.TMX
{
    // 来源 https://github.com/mapeditor/tiled/blob/master/docs/reference/tmx-map-format.rst

    [XmlType("map", AnonymousType = true)]
    [XmlRoot("map", IsNullable = false)]
    public class Map
    {
        [XmlAttribute("version")]
        public string Version;

        [XmlAttribute("tiledversion")]
        public string TiledVersion;

        [XmlAttribute("class")]
        public string Class;

        [XmlAttribute("renderorder")]
        public RenderOrderEnum RenderOrder;

        [XmlAttribute("compressionlevel")]
        public int CompressionLevel;

        [XmlAttribute("orientation")]
        public string Orientation;

        [XmlAttribute("width")]
        public int Width;

        [XmlAttribute("height")]
        public int Height;

        [XmlAttribute("tilewidth")]
        public int TileWidth;

        [XmlAttribute("tileheight")]
        public int TileHeight;

        [XmlAttribute("hexsidelength")]
        public int HexSideLength;

        [XmlAttribute("staggeraxis")]
        public StaggerAxisEnum StaggerAxis;

        [XmlAttribute("staggerindex")]
        public StaggerIndexEnum StaggerIndex;

        [XmlAttribute("parallaxoriginx")]
        public int ParallaxOriginX;

        [XmlAttribute("parallaxoriginy")]
        public int ParallaxOriginY;

        [XmlAttribute("backgroundcolor")]
        public string BackgroundColor;

        [XmlAttribute("nextlayerid")]
        public int NextLayerID;

        [XmlAttribute("nextobjectid")]
        public int NextObjectID;

        [XmlAttribute("infinite")]
        public BoolEnum Infinite;

        //[XmlChoiceIdentifier()]
        [XmlElement("tileset", typeof(TileSet))]
        [XmlElement("layer", typeof(Layer))]
        [XmlElement("objectgroup", typeof(ObjectGroup))]
        [XmlElement("imagelayer", typeof(ImageLayer))]
        [XmlElement("group", typeof(Group))]
        public List<MapChild> Layers;

        [XmlElement("properties")]
        public Properties Properties;

        [XmlElement("editorsettings")]
        public EditorSettings EditorSettings;

        #region ctors

        public Map()
        {
            Layers = new List<MapChild>();
            Properties = new Properties();
        }

        #endregion

        #region overrides

        public override string ToString()
        {
            return $"[map] Width:{Width}, Height:{Height}, TileWidth: {TileWidth}, TileHeight: {TileHeight}, LayerCount:{Layers.Count}";
        }

        #endregion

        #region statics

        public static Map Load(string path)
        {
            using (var filestream = new FileStream(path, System.IO.FileMode.Open))
            {
                return Load(filestream);
            }
        }

        public static Map Load(System.IO.Stream stream)
        {
            var xmlSeri = new XmlSerializer(typeof(Map));
            var map = xmlSeri.Deserialize(stream) as Map;
            return map;
        }


        #endregion


    }
}
